<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>AUI快速完成布局</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />


</head>

<body>
  <div style="text-align:center;margin:0.625rem;display:none;background:#fff;padding:1.25rem;" id="default-1">
    <img src="./img/muyou.png" style="display:inline;">
      <p><span style="font-size:1.25rem;">亲,</span>&nbsp;&nbsp;&nbsp;&nbsp;您当前木有对应的信息</p>
  </div>
    <div style="padding:20px;">

        <div id="list"></div>

    </div>


</body>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="aui-row" style="border-bottom:1px solid #ccc" onclick="fnEditTaobaoBind(this)" data-id="{{= value.id}}">
        <div class="aui-col-xs-4">
            <div style="line-height:40px;color:green;overflow:hidden">
                {{= value.account}}
            </div>
        </div>
        <div class="aui-col-xs-4">

            <div style="line-height:40px;padding-top:10px;">
                <img src="{{=fnSiteUrl(value.account_level)}}" alt="" />

            </div>
        </div>
        <div class="aui-col-xs-4 ">

             {{? value.is_default==1}}<div style="line-height:40px;color:red;height:40px;">默认使用账号</div>{{?}}


        </div>

    </div>

    {{~}}
</script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="./conf/config.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript">
    apiready = function() {

        fnGetListTaobaoBind(); //获取用户信息分配到模板
        initRefresh() ;//下拉

    };
    function initRefresh() {
        api.setRefreshHeaderInfo({
            visible: true,
            loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...',
            showTime: true
        }, function(ret, err) {
            fnGetListTaobaoBind();
        });
    }

    function fnGetListTaobaoBind() {
        var userInfo = $api.getStorage("userInfo");
        var userid = userInfo.userid;
        var random = userInfo.random;
        var type = 1;//京东是1淘宝是0
        api.ajax({
            url: SITE_URL+'index.php?m=api&c=appnew&a=get_tbaccount',
            method: 'post',
            data: {
                values: {
                    "userid": userid,
                    "random": random,
                    "type": 1
                }
            }
        }, function(ret, err) {
          switch (ret.status) {
            case 0:
                alert(ret.msg);
                api.refreshHeaderLoadDone(); //下拉刷新隐藏
                break;
              case 1:
                fnUpdataListTaobaoBind(ret);
                  api.refreshHeaderLoadDone(); //下拉刷新隐藏
                  break;
              case 2:
                  alert(ret.msg);
                  api.openWin({
                      name: 'login',
                      url: './login.html',
                      pageParam: {
                          name: 'test'
                      }
                  });

                  //然后关闭当前网页，跳转到另一个网页；
                  break;
              case 5:
                  fnUpdateListDefault();
                  api.refreshHeaderLoadDone(); //下拉刷新隐藏
                  break;
          }


            //alert(JSON.stringify(ret));

        });
    };
    //把数据分配到模板上面
    function fnUpdateListDefault() {
        $("#default-1").show();
    }

    function fnUpdataListTaobaoBind(ret) {
        //数组分配到模板；


        var list = $api.byId('list'); //获取节点
        //var str={"array":["banana","apple","orange"]};
        // 1. 编译模板函数
        var tempFn = doT.template($api.byId('template').innerHTML); //找到模板多次使用
        // 2. 多次使用模板函数
        var resultText = tempFn(ret.lists);

        $api.html(list, resultText);


    }


    function initEventListenner() {

        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            api.confirm({
                title: '提示',
                msg: '是否退出应用',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    api.closeWidget({
                        silent: true
                    });
                }
            });
        });
    };
    //点击编辑绑定淘宝账号
    function fnEditTaobaoBind(that) {
        var id = $(that).attr("data-id");
        api.actionSheet({
            title: '你想要进行操作',
            cancelTitle: '取消',
            buttons: ['设为默认值', '解除绑定']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                fnSetDefault(id);
            }
            if (index == 2) {
                fnRelieveTaobaoBind(id);
            }

        });

    }
    //点击淘宝账号设置默认值
    function fnSetDefault(id) {
        var userInfo = $api.getStorage('userInfo');

        //获取数据app中存入的数据然后分配到模板中；
        var random = userInfo.random;
        var userid = userInfo.userid;

        api.ajax({
                url: SITE_URL+'index.php?m=api&c=appnew&a=setdefault',
                method: 'POST',
                data: {
                    values: {
                        "id": id,
                        "userid": userid,
                        "random": random,
                        "type": "jd"
                    }
                }
            }, function(ret, err) {
                if (ret.status == 1) {
                    alert(ret.msg);
                    fnGetListTaobaoBind();
                    //var df="#df"+id;
                    //$(df).html()
                } else {
                    alert(JSON.stringify(err));
                }


        });
    }
    //淘宝账号解除绑定
    function fnRelieveTaobaoBind(id) {
        var userInfo = $api.getStorage('userInfo');

        //获取数据app中存入的数据然后分配到模板中；
        var random = userInfo.random;
        var userid = userInfo.userid;

        api.ajax({
                url: SITE_URL+'index.php?m=api&c=appnew&a=bind_del_tb',
                method: 'POST',
                data: {
                    values: {
                        "id": id,
                        "userid": userid,
                        "random": random,

                    }
                }
            }, function(ret, err) {
                if (ret.status == 1) {
                    alert(ret.msg);
                    fnGetListTaobaoBind();
                } else {
                    alert(JSON.stringify(err));
                }

        })

    }
</script>

</html>
